
using System;
using System.Collections.Generic;
using System.Text;
using System.Configuration;
using System.Data;
using System.Data.Common;
using Gman.Entity.Entities;
using Gman.Entity.Interfaces;
using Gman.Common;
using Microsoft.Practices.EnterpriseLibrary.Data;


namespace Gman.Data
{
    //-------------------------------------------------------------------
    //Archivo     : ActaServNoConformeDA.cs
    //Proyecto    : Gman
    //Creacion    : BIS(NombreDesarrollador) 20130226
    //Modificacion: 
    //Descripcion : Clase para capa de datos.
    //-------------------------------------------------------------------
    public class ActaServNoConformeDA : IActaServNoConforme
    {

        Database db = DatabaseFactory.CreateDatabase();

        //-----------------------------------------------------------------------------------------------
        //Nombre: Listar
        //Objetivo: Retorna una lista de registros de tipo ActaServNoConformeBE  
        //Creacion    : TISI(NombreDesarrollador) 20130226  
        //-----------------------------------------------------------------------------------------------
        public IList<ActaServNoConformeBE> Listar(ActaServNoConformeBE oItem)
        {
            IList<ActaServNoConformeBE> lista = new List<ActaServNoConformeBE>();
            try
            {
                DbCommand cmd = db.GetStoredProcCommand("SP_ListarActaServNoConforme");

                using (IDataReader oR = db.ExecuteReader(cmd))
                {
                    lista = MakeUniqueDatos(oR);
                }

            }
            catch (Exception ex)
            {
                throw new Exception("ActaServNoConformeDA.Listar()", ex);
            }
            finally
            {
            }
            return lista;
        }

        //-----------------------------------------------------------------------------------------------
        //Nombre: Obtener
        //Objetivo: Retorna un objeto de Tipo ActaServNoConformeBE  
        //Creacion    : BIS(NombreDesarrollador) 20130226  
        //-----------------------------------------------------------------------------------------------
        public ActaServNoConformeBE Obtener(ActaServNoConformeBE oItem)
        {
            ActaServNoConformeBE itemResult = null;
            try
            {
                DbCommand cmd = db.GetStoredProcCommand("SP_ObtenerActaServNoConforme", oItem.IDActaServNoConfor, "");

                using (IDataReader oR = db.ExecuteReader(cmd))
                {
                    while (oR.Read())
                    {
                        itemResult = MakeDatosMapeados(oR);
                    }
                }
                return itemResult;
            }
            catch (Exception ex)
            {
                throw new Exception("ActaServNoConformeDA.Obtener()", ex);
            }
            finally
            {
            }
        }

        //-----------------------------------------------------------------------------------------------
        //Nombre: Registrar
        //Objetivo: Almacena el registro de un objeto de tipo ActaServNoConformeBE  
        //Creacion    : TISI(NombreDesarrollador) 20130226  
        //----------------------------------------------------------------------------------------------
        public int Registrar(ActaServNoConformeBE oItem)
        {
            int resultado = 0;
            try
            {
                DbCommand cmd = db.GetStoredProcCommand("SP_RegistrarActaServNoConforme");
                db.AddInParameter(cmd, "@IDActaServNoConfor", DbType.Int64, oItem.IDActaServNoConfor);
                db.AddInParameter(cmd, "@IDOt", DbType.Int64, oItem.IDOt);
                db.AddInParameter(cmd, "@IDProveedor", DbType.Int64, oItem.IDProveedor);
                db.AddInParameter(cmd, "@CodAsnc", DbType.String, oItem.CodAsnc);
                db.AddInParameter(cmd, "@IDSede", DbType.Int64, oItem.IDSede);
                db.AddInParameter(cmd, "@TipoOperacionAsnc", DbType.String, oItem.TipoOperacionAsnc);
                db.AddInParameter(cmd, "@DescOperAsnc", DbType.String, oItem.DescOperAsnc);
                db.AddInParameter(cmd, "@MtvoAsnc", DbType.String, oItem.MtvoAsnc);
                db.AddInParameter(cmd, "@AccTomadaAsnc", DbType.String, oItem.AccTomadaAsnc);
                db.AddInParameter(cmd, "@SupAsnc", DbType.String, oItem.SupAsnc);
                db.AddInParameter(cmd, "@TecAsnc", DbType.String, oItem.TecAsnc);
                db.AddInParameter(cmd, "@IDEmp", DbType.Int32, oItem.IDEmp);
                db.AddInParameter(cmd, "@FecRegisAsnc", DbType.DateTime, oItem.FecRegisAsnc);




                resultado = Convert.ToInt32(db.ExecuteScalar(cmd));
                //resultado = db.ExecuteNonQuery(cmd);      

            }
            catch (Exception ex)
            {
                throw new Exception("ActaServNoConformeDA.Registrar()", ex);
            }
            finally
            {

            }
            return resultado;
        }

        //-----------------------------------------------------------------------------------------------
        //Nombre: Actualizar
        //Objetivo: Actualiza el registro de un objeto de tipo ActaServNoConformeBE 
        //Creacion    : TISI(NombreDesarrollador) 20130226  
        //-----------------------------------------------------------------------------------------------
        public int Actualizar(ActaServNoConformeBE oItem)
        {
            int resultado = 0;
            try
            {
                DbCommand cmd = db.GetStoredProcCommand("SP_ActualizarActaServNoConforme");
                db.AddInParameter(cmd, "@IDActaServNoConfor", DbType.Int64, oItem.IDActaServNoConfor);
                db.AddInParameter(cmd, "@IDOt", DbType.Int64, oItem.IDOt);
                db.AddInParameter(cmd, "@IDProveedor", DbType.Int64, oItem.IDProveedor);
                db.AddInParameter(cmd, "@CodAsnc", DbType.String, oItem.CodAsnc);
                db.AddInParameter(cmd, "@IDSede", DbType.Int64, oItem.IDSede);
                db.AddInParameter(cmd, "@TipoOperacionAsnc", DbType.String, oItem.TipoOperacionAsnc);
                db.AddInParameter(cmd, "@DescOperAsnc", DbType.String, oItem.DescOperAsnc);
                db.AddInParameter(cmd, "@MtvoAsnc", DbType.String, oItem.MtvoAsnc);
                db.AddInParameter(cmd, "@AccTomadaAsnc", DbType.String, oItem.AccTomadaAsnc);
                db.AddInParameter(cmd, "@SupAsnc", DbType.String, oItem.SupAsnc);
                db.AddInParameter(cmd, "@TecAsnc", DbType.String, oItem.TecAsnc);
                db.AddInParameter(cmd, "@IDEmp", DbType.Int32, oItem.IDEmp);
                //db.AddInParameter(cmd, "@FecRegisAsnc", DbType.DateTime, oItem.FecRegisAsnc);

                resultado = db.ExecuteNonQuery(cmd);

            }
            catch (Exception ex)
            {
                throw new Exception("ActaServNoConformeDA.Actualizar()", ex);
            }
            finally
            {

            }
            return resultado;
        }

        //-----------------------------------------------------------------------------------------------
        //Nombre: Eliminar
        //Objetivo: Elimina el registro de un objeto de tipo ActaServNoConformeBE  
        //Creacion    : BIS(NombreDesarrollador) 20130226  
        //-----------------------------------------------------------------------------------------------
        public int Eliminar(ActaServNoConformeBE oItem)
        {
            int resultado = 0;
            try
            {
                DbCommand cmd = db.GetStoredProcCommand("SP_EliminarActaServNoConforme");
                db.AddInParameter(cmd, "@IDActaServNoConfor", DbType.Int64, oItem.IDActaServNoConfor);


                resultado = db.ExecuteNonQuery(cmd);

            }
            catch (Exception ex)
            {
                throw new Exception("ActaServNoConformeDA.Eliminar()", ex);
            }
            finally
            {

            }
            return resultado;
        }





        public ActaServNoConformeBE ObtenerXNroAsnc(ActaServNoConformeBE oItem)
        {
            ActaServNoConformeBE itemResult = null;
            try
            {
                DbCommand cmd = db.GetStoredProcCommand("SP_ObtenerActaServNoConformexCod");
                db.AddInParameter(cmd, "@CodAsnc", DbType.String, oItem.CodAsnc);
                db.AddInParameter(cmd, "@IDEmp", DbType.Int32, oItem.IDEmp);

                using (IDataReader oR = db.ExecuteReader(cmd))
                {
                    while (oR.Read())
                    {
                        itemResult = MakeDatosMapeados(oR);
                    }
                }
                return itemResult;
            }
            catch (Exception ex)
            {
                throw new Exception("ActaServNoConformeDA.Obtener()", ex);
            }
            finally
            {
            }
        }




        private IList<ActaServNoConformeBE> MakeUniqueDatos(IDataReader oR)
        {
            IList<ActaServNoConformeBE> list = new List<ActaServNoConformeBE>();

            while (oR.Read())
                list.Add(MakeDatosMapeados(oR));

            return list;
        }

        private ActaServNoConformeBE MakeDatosMapeados(IDataReader oR)
        {
            var itemResult = new ActaServNoConformeBE();

            itemResult.IDActaServNoConfor = oR["IDActaServNoConfor"].ToInt64();
            itemResult.IDOt = oR["IDOt"].ToInt64();
            itemResult.IDProveedor = oR["IDProveedor"].ToInt64();
            itemResult.CodAsnc = oR["CodAsnc"].ToString();
            itemResult.IDSede = oR["IDSede"].ToInt64();
            itemResult.TipoOperacionAsnc = oR["TipoOperacionAsnc"].ToString();
            itemResult.DescOperAsnc = oR["DescOperAsnc"].ToString();
            itemResult.MtvoAsnc = oR["MtvoAsnc"].ToString();
            itemResult.AccTomadaAsnc = oR["AccTomadaAsnc"].ToString();
            itemResult.SupAsnc = oR["SupAsnc"].ToString();
            itemResult.TecAsnc = oR["TecAsnc"].ToString();
            itemResult.IDEmp = oR["IDEmp"].ToInt32();
            itemResult.FecRegisAsnc = oR["FecRegisAsnc"].ToDateTime();


            if (oR["NroOt"].ToString() != null && oR["NroOt"].ToString() != string.Empty)
            {
                if (itemResult.Ot == null) itemResult.Ot = new OrdenTrabajoBE();
                itemResult.Ot.NroOt = oR["NroOt"].ToString();

            }
            if (oR["RazonSProv"].ToString() != null && oR["RazonSProv"].ToString() != string.Empty)
            {
                if (itemResult.Ot.proveedor  == null) itemResult.Ot.proveedor = new ProveedorBE  ();
                itemResult.Ot.proveedor.RazonSProv = oR["RazonSProv"].ToString();

            }

           

            return itemResult;
        }
    }
}
